﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Centrica.Task.Model;

namespace Centrica.Task.App.Model
{
    public class TaskProxy : Centrica.Task.Model.Task
    {
        public TaskProxy()
            : base()
        {
        }

        public TaskProxy(Centrica.Task.Model.Task task)
        {
            this.Description = task.Description;
            this.ID = task.ID;
            this.Name = task.Name;
            this.Owner = task.Owner;
            this.Status = task.Status;
            this.StartDate = task.StartDate;
            this.FinishDate = task.FinishDate;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class TaskModelProvider : ITaskProvider
    {
        static private List<TaskProxy> _taskList = null;

        public TaskModelProvider()
        {
            if (TaskModelProvider._taskList == null)
            {
                TaskModelProvider._taskList = new List<TaskProxy>()
                {
                    new TaskProxy()
                    {
                        Name = "Task 1",
                        Description = "This is a Task 1",
                        Owner = "Mark Owen",
                        StartDate = DateTime.Now.AddDays(-1),
                        FinishDate = DateTime.Now.AddDays(1),
                        Status = TaskStatus.Unauthorised
                    },
                    new TaskProxy()
                    {
                        Name = "Task 2",
                        Description = "This is a Task 2",
                        Owner = "Glen Keaton",
                        StartDate = DateTime.Now.AddDays(-2),
                        FinishDate = DateTime.Now.AddDays(2),
                        Status = TaskStatus.Initiated
                    },
                    new TaskProxy()
                    {
                        Name = "Task 3",
                        Description = "This is a Task 3",
                        Owner = "Steve Matthews",
                        StartDate = DateTime.Now.AddDays(-3),
                        FinishDate = DateTime.Now.AddDays(3),
                        Status = TaskStatus.Completed
                    }
                };
            }
        }

        private List<Centrica.Task.Model.Task> TaskList
        {
            get
            {
                return (TaskModelProvider._taskList
                    .Select(t => (t as Centrica.Task.Model.Task)).ToList());
            }
        }

        /// <summary>
        /// ITaskProvider implementation
        /// </summary>
        public List<Centrica.Task.Model.Task> GetAllTasks()
        {
            return TaskList;
        }

        public List<Centrica.Task.Model.Task> GetByTemplate(Centrica.Task.Model.Task template)
        {
            return TaskList;
        }

        public Centrica.Task.Model.Task Find(
            Func<Centrica.Task.Model.Task, Boolean> predicate)
        {
            return TaskList
                .First(predicate);
        }

        public void AddNewTask(Centrica.Task.Model.Task task)
        {
            TaskModelProvider._taskList.Add(new TaskProxy(task));
        }

        public void UpdateTask(Centrica.Task.Model.Task task)
        {
            Centrica.Task.Model.Task taskOrg =
                TaskModelProvider._taskList.First(t => (t.ID == task.ID));
            taskOrg.Name = task.Name;
            taskOrg.Description = task.Description;
            taskOrg.Owner = task.Owner;
            taskOrg.StartDate = task.StartDate;
            taskOrg.FinishDate = task.FinishDate;
            taskOrg.Status = task.Status;
        }

        public void RemoveTask(Centrica.Task.Model.Task task)
        {
            TaskModelProvider._taskList.RemoveAll(t => (t.ID == task.ID));
        }

    }
}
